首页 > 试题广场 >

从尾到头打印链表

[编程题]从尾到头打印链表
  • 热度指数:1695665 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。

输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]

0 <= 链表长度 <= 10000
示例1

输入

{1,2,3}

输出

[3,2,1]
示例2

输入

{67,0,24,58}

输出

[58,24,0,67]

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
推荐
方法一:链表从尾到头输出,利用递归实现,不使用库函数直接printf输出的时候用递归比较好
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        vector<int> value;
        if(head != NULL)
        {
            value.insert(value.begin(),head->val);
            if(head->next != NULL)
            {
                vector<int> tempVec = printListFromTailToHead(head->next);
                if(tempVec.size()>0)
                value.insert(value.begin(),tempVec.begin(),tempVec.end());  
            }         
            
        }
        return value;
    }
};
方法二:用库函数,每次扫描一个节点,将该结点数据存入vector中,如果该节点有下一节点,将下一节点数据直接插入vector最前面,直至遍历完,或者直接加在最后,最后调用reverse
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        vector<int> value;
        if(head != NULL)
        {
            value.insert(value.begin(),head->val);
            while(head->next != NULL)
            {
                value.insert(value.begin(),head->next->val);
                head = head->next;
            }         
            
        }
        return value;
    }
};

编辑于 2015-06-18 16:53:34 回复(73)
顺序访问链表后输出比较常见

但是翻转链表也是非常经典的,,这里可以将链表先进行翻转后顺序访问

function ListNode(x){
    this.val = x;
    this.next = null;
}
function printListFromTailToHead(head)
{
  let reverseHead = new ListNode(-1)
  // 翻转链表
  while (head !== null) {
    const pre = head
    // 下一个结点
    head = head.next
    pre.next = reverseHead.next
    reverseHead.next = pre
  }
  // 重新整理 去掉val=-1的点
  reverseHead = reverseHead.next

  const result = []
  // 遍历链表
  while (reverseHead !== null) {
    result.push(reverseHead.val)
    // 下一个结点
    reverseHead = reverseHead.next
  }
  return result
}
module.exports = {
    printListFromTailToHead : printListFromTailToHead
};


发表于 2023-03-27 00:36:48 回复(0)
//方法一  使用数组的unshift方法 只要head不为空就在数组的前面插入元素
function printListFromTailToHead(head)
{  
let arr=[]
while(head){
    arr.unshift(head.val)
    
head=head.next
}
return arr
}
module.exports = {
    printListFromTailToHead : printListFromTailToHead
};
发表于 2022-06-27 16:25:32 回复(0)
const arrayList = {
        value: 1,
        next: {
            value: 2,
            next: {
                value: 3,
                next: null
            }
        }
 }

function printListFromTailToHead(head) {
        let arr = [];
        while(head) {
            arr.unshift(head.value);
            head = head.next;
        }
        return arr;
    }

发表于 2021-10-25 16:25:48 回复(0)
简洁的JavaScript:
function printListFromTailToHead(head)
{
    // write code here
    let arr = [];
    while(head){
        arr.unshift(head.val);
        head = head.next;
    }
    return arr;
}


发表于 2021-09-27 18:15:53 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    let arr=[]
    while(head){
        arr.unshift(head.val)
        head=head.next
    }
    return arr
}

发表于 2021-09-10 14:44:40 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    let arr = [];
    while(head) {
        arr.push(head.val);
        head = head.next;
    }
    return arr.reverse();
}

发表于 2021-09-09 22:22:12 回复(0)